package com.soft.sitegenerator.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.soft.sitegenerator.business.hibernate.Role;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;
import com.soft.sitegenerator.form.RoleEditForm;

public class RoleEditAction  extends DispatchAction {

	public ActionForward editRole(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		RoleEditForm roleEditForm = (RoleEditForm) form;
		
	    Long id = new Long(Long.parseLong(request.getParameter("id")));
		
	    Role role = roleEditForm.getRole();
	    role.setId(id);

	    try {
			role.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}
	    
		return mapping.findForward("showEdit");
	}
	
	public ActionForward deleteRole(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		RoleEditForm roleEditForm = (RoleEditForm) form;

	    Long id = new Long(Long.parseLong(request.getParameter("id")));
		
	    Role role = roleEditForm.getRole();
	    role.setId(id);
		role.delete();
		
		return mapping.findForward("showList");
	}
	
	public ActionForward addRole(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		
		return mapping.findForward("showAdd");
	}
	
	public ActionForward saveRole(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		RoleEditForm roleEditForm = (RoleEditForm) form;
		
	    Role role = roleEditForm.getRole();
		role.save();
		
		return mapping.findForward("showList");
		
	}

	public ActionForward updateRole(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) {
			RoleEditForm roleEditForm = (RoleEditForm) form;
			
		    Role user = roleEditForm.getRole();
			user.update();
			
			return mapping.findForward("showList");
			
		}

}